package com.red.wood.module.evalMol.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.inject.internal.Lists;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.module.eval.model.SingleTask;
import com.red.wood.module.eval.model.TaskAudit;
import com.red.wood.module.evalMol.model.AdoptCdn;
import com.red.wood.module.evalMol.model.AdoptIsp;
import com.red.wood.module.evalMol.model.AdoptNotInt;
import com.red.wood.module.evalMol.model.AdoptOnlyInt;
import com.red.wood.module.evalMol.model.GbpsMol;
import com.red.wood.module.evalMol.model.HouseMol;
import com.red.wood.module.evalMol.model.IcpMol;
import com.red.wood.module.evalMol.model.NotAdoptInfo;
import com.red.wood.module.evalMol.model.NotAdoptMol;
import com.red.wood.module.evalMol.service.AdoptCdnService;
import com.red.wood.module.evalMol.service.AdoptIspService;
import com.red.wood.module.evalMol.service.AdoptNotIntService;
import com.red.wood.module.evalMol.service.GbpsMolService;
import com.red.wood.module.evalMol.service.HouseMolService;
import com.red.wood.module.evalMol.service.IcpMolService;
import com.red.wood.module.evalMol.service.MolService;
import com.red.wood.module.evalMol.service.NotAdoptInfoService;
import com.red.wood.module.evalMol.service.ReportMolService;

@Controller
@RequestMapping("/reportMol")
public class ReportMolController {
	
	private static final Log log = LogFactory.getLog(ReportMolController.class);
	
	@Autowired
	private ReportMolService reportMolService;
	@Autowired
	private AdoptIspService  adoptIspService;
	@Autowired
	private AdoptNotIntService adoptNotIntService;
	@Autowired
	private AdoptCdnService adoptCdnService;
	@Autowired
	private GbpsMolService gbpsMolService;
	@Autowired
	private HouseMolService houseMolService;
	@Autowired
	private IcpMolService icpMolService;
	@Autowired
	private NotAdoptInfoService notAdoptInfoService;
	@Autowired
	private MolService molService;
	
	/**
	 * 获取模板数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "getMolData", method = RequestMethod.POST)
	@ResponseBody
	public String getMolData(HttpServletRequest request, HttpServletResponse response){
		JSONObject json = new JSONObject();
		try{
			String singleTaskId = CommonUtils.valueOf(request.getParameter("singleTaskId"));
			String type = CommonUtils.valueOf(request.getParameter("type"));
			String statue = CommonUtils.valueOf(request.getParameter("statue"));
			String backViewType = CommonUtils.valueOf(request.getParameter("backViewType"));
			
			if((Integer.valueOf(statue) == TaskAudit.EVAL_TASTAUDIT_STATUE_NOTADOPT && Integer.valueOf(backViewType) == 2) 
					|| (Integer.valueOf(statue) == SingleTask.EVAL_SINGLETASK_STATUE_NOTADOPT && Integer.valueOf(backViewType) == 1)){ // 不通过
				json.put("notAdoptData", notAdoptInfoService.getBySingleTaskId(singleTaskId));
			} else if((Integer.valueOf(statue) == TaskAudit.EVAL_TASTAUDIT_STATUE_ADOPT && Integer.valueOf(backViewType) == 2)
					|| (Integer.valueOf(statue) == SingleTask.EVAL_SINGLETASK_STATUE_ADOPT && Integer.valueOf(backViewType) == 1)){ // 通过
				if(Integer.valueOf(type) ==  SingleTask.EVAL_SINGLETASK_TYPE_ONLYINTER){ // idc业务评测(仅限互联网资源协作)
					json.put("houseData", houseMolService.getBySingleTaskId(singleTaskId));
					json.put("gbpsData", gbpsMolService.getBySingleTaskId(singleTaskId));
				} else if(Integer.valueOf(type) ==  SingleTask.EVAL_SINGLETASK_TYPE_ISP) { // ISP业务评测 
					json.put("ispData", adoptIspService.getBySingleTaskId(singleTaskId));
				} else if(Integer.valueOf(type) ==  SingleTask.EVAL_SINGLETASK_TYPE_CDN) { // CDN业务评测
					json.put("gbpsData", gbpsMolService.getBySingleTaskId(singleTaskId));
					json.put("houseData", houseMolService.getBySingleTaskId(singleTaskId));
					json.put("icpData", icpMolService.getBySingleTaskId(singleTaskId));
					json.put("cdnMolData", adoptCdnService.getBySingleTaskId(singleTaskId));
				} else {
					json.put("adoptNotIntData", adoptNotIntService.getBySingleTaskId(singleTaskId));
				}
			}
			
			json.put("flag", true);
			json.put("data", reportMolService.getBySingleTaskId(singleTaskId));
		}catch(Exception e){
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}
		return json.toString();
	}

	@RequestMapping(value = "upCdnMolData", method = RequestMethod.POST)
	@ResponseBody
	public String upCdnMolData(AdoptCdn cdn){
		JSONObject json = new JSONObject();
		try{
			this.setHouseData(cdn.getHouseList());
			this.setGbpsData(cdn.getGbpsList());
			this.setIcpData(cdn.getIcpList());
			this.setCdnToBean(cdn);
			json.put("flag", true);
		}catch(Exception e){
			json.put("flag", false);
		}
		return json.toString();
	}
	
	@RequestMapping(value = "upIspMolData", method = RequestMethod.POST)
	@ResponseBody
	public String upIspMolData(AdoptIsp isp){
		JSONObject json = new JSONObject();
		try{
			this.setIspToBean(isp);
			json.put("flag", true);
		}catch(Exception e){
			json.put("flag", false);
		}
		return json.toString();
	}
	
	@RequestMapping(value = "upNotIntMolData", method = RequestMethod.POST)
	@ResponseBody
	public String upNotIntMolData(AdoptNotInt notInt){
		JSONObject json = new JSONObject();
		try{
			this.setNotIntToBean(notInt);
			json.put("flag", true);
		}catch(Exception e){
			json.put("flag", false);
		}
		return json.toString();
	}
	
	@RequestMapping(value = "upOnlyIntMolData", method = RequestMethod.POST)
	@ResponseBody
	public String upOnlyIntMolData(AdoptOnlyInt onlyInt){
		JSONObject json = new JSONObject();
		try{
			this.setHouseData(onlyInt.getHouseList());
			this.setGbpsData(onlyInt.getGbpsList());
			this.setOnlyIntToBean(onlyInt);
			json.put("flag", true);
		} catch(Exception e){
			json.put("flag", false);
		}
		return json.toString();
	}
	
	@RequestMapping(value = "upNotAdoptMolData", method = RequestMethod.POST)
	@ResponseBody
	public String upNotAdoptMolData(NotAdoptMol notAdopt){
		JSONObject json = new JSONObject();
		try{
			this.setNotAdroptData(notAdopt.getInfoList());
			this.setNotAdoptToBean(notAdopt);
			json.put("flag", true);
		}catch(Exception e){
			json.put("flag", false);
		}
		return json.toString();
	}
	
	private void setHouseData(List<HouseMol> houseList){
		List<Map<String, Object>> dataList = Lists.newArrayList();
		Map<String, Object> map = new HashMap<>();
		
		if(houseList != null && houseList.size() > 0){
			String singleTaskId = houseList.get(0).getSingleTaskId();
			
			for(HouseMol h: houseList){
				map.put("houseAddr", h.getHouseAddr());
				map.put("nodeName", h.getNodeName());
				map.put("singleTaskId", h.getSingleTaskId());
				dataList.add(map);
			}
			houseMolService.delete(singleTaskId);
			houseMolService.addList(dataList);
		}
	}
	
	private void setIcpData(List<IcpMol> icpList){
		List<Map<String, Object>> dataList = Lists.newArrayList();
		Map<String, Object> map = new HashMap<>();
		
		if(icpList != null && icpList.size() > 0){
			String singleTaskId = icpList.get(0).getSingleTaskId();
			
			for(IcpMol i: icpList){
				map.put("domain", i.getDomain());
				map.put("icp", i.getIcp());
				map.put("singleTaskId", i.getSingleTaskId());
				dataList.add(map);
			}
			icpMolService.delete(singleTaskId);
			icpMolService.addList(dataList);
		}
	}
	
	private void setGbpsData(List<GbpsMol> gbpsList){
		List<Map<String, Object>> dataList = Lists.newArrayList();
		Map<String, Object> map = new HashMap<>();
		
		if(gbpsList != null && gbpsList.size() > 0){
			String singleTaskId = gbpsList.get(0).getSingleTaskId();
			
			for(GbpsMol g: gbpsList){
				map.put("gbps", g.getGbps());
				map.put("province", g.getProvince());
				map.put("singleTaskId", singleTaskId);
				map.put("type", g.getType());
				dataList.add(map);
			}
			gbpsMolService.delete(singleTaskId);
			gbpsMolService.addList(dataList);
		}
	}
	
	private void setNotAdroptData(List<NotAdoptInfo> notAdoptInfoList){
		List<Map<String, Object>> dataList = Lists.newArrayList();
		Map<String, Object> map = new HashMap<>();
		
		if(notAdoptInfoList != null && notAdoptInfoList.size() > 0){
			String singleTaskId = notAdoptInfoList.get(0).getSingleTaskId();
			
			for(NotAdoptInfo n: notAdoptInfoList){
				map.put("problem", n.getProblem());
				map.put("singleTaskId", singleTaskId);
				dataList.add(map);
			}
			notAdoptInfoService.delete(singleTaskId);
			notAdoptInfoService.addList(dataList);
		}
	}
	
	private void setNotAdoptToBean(NotAdoptMol notAdopt){
		Map<String, Object> map = new HashMap<>();
		map.put("number", notAdopt.getNumber());
		map.put("singleTaskId", notAdopt.getSingleTaskId());
		map.put("name", notAdopt.getName());
		map.put("systemName", notAdopt.getSystemName());
		map.put("handleDay", notAdopt.getHandleDay());
		map.put("handleMonth", notAdopt.getHandleMonth());
		map.put("handleYear", notAdopt.getHandleYear());
		map.put("deptPerson", notAdopt.getDeptPerson());
		map.put("personTel", notAdopt.getPersonTel());
		reportMolService.delete(notAdopt.getSingleTaskId());
		reportMolService.add(map);
	}
	
	private void setCdnToBean(AdoptCdn cdn){
		Map<String, Object> map = new HashMap<>();
		map.put("number", cdn.getNumber());
		map.put("singleTaskId", cdn.getSingleTaskId());
		map.put("name", cdn.getName());
		map.put("systemName", cdn.getSystemName());
		map.put("handleDay", cdn.getHandleDay());
		map.put("handleMonth", cdn.getHandleMonth());
		map.put("handleYear", cdn.getHandleYear());
		map.put("deptPerson", cdn.getDeptPerson());
		map.put("personTel", cdn.getPersonTel());
		reportMolService.delete(cdn.getSingleTaskId());
		reportMolService.add(map);
	}
	
	private void setIspToBean(AdoptIsp isp){
		Map<String, Object> map = new HashMap<>();
		map.put("number", isp.getNumber());
		map.put("singleTaskId", isp.getSingleTaskId());
		map.put("name", isp.getName());
		map.put("systemName", isp.getSystemName());
		map.put("handleDay", isp.getHandleDay());
		map.put("handleMonth", isp.getHandleMonth());
		map.put("handleYear", isp.getHandleYear());
		map.put("deptPerson", isp.getDeptPerson());
		map.put("personTel", isp.getPersonTel());
		reportMolService.delete(isp.getSingleTaskId());
		reportMolService.add(map);
		
		Map<String, Object> map2 = new HashMap<>();
		map2.put("singleTaskId", isp.getSingleTaskId());
		map2.put("gameProvince", isp.getGameProvince());
		map2.put("houseName", isp.getHouseName());
		map2.put("houseAddress", isp.getHouseAddress());
		map2.put("handleInfo", isp.getHandleInfo());
		map2.put("gbps", isp.getGbps());
		adoptIspService.delete(isp.getSingleTaskId());
		adoptIspService.add(map2);
	}
	
	private void setNotIntToBean(AdoptNotInt notInt){
		Map<String, Object> map = new HashMap<>();
		map.put("number", notInt.getNumber());
		map.put("singleTaskId", notInt.getSingleTaskId());
		map.put("name", notInt.getName());
		map.put("systemName", notInt.getSystemName());
		map.put("handleDay", notInt.getHandleDay());
		map.put("handleMonth", notInt.getHandleMonth());
		map.put("handleYear", notInt.getHandleYear());
		map.put("deptPerson", notInt.getDeptPerson());
		map.put("personTel", notInt.getPersonTel());
		reportMolService.delete(notInt.getSingleTaskId());
		reportMolService.add(map);
		
		Map<String, Object> map2 = new HashMap<>();
		map2.put("singleTaskId", notInt.getSingleTaskId());
		map2.put("linkTypeAndGbps", notInt.getLinkTypeAndGbps());
		map2.put("houseName", notInt.getHouseName());
		map2.put("houseAddress", notInt.getHouseAddress());
		map2.put("handleInfo", notInt.getHandleInfo());
		map2.put("provinceAndCity", notInt.getProvinceAndCity());
		adoptNotIntService.delete(notInt.getSingleTaskId());
		adoptNotIntService.add(map2);
	}
	
	private void setOnlyIntToBean(AdoptOnlyInt onlyInt){
		Map<String, Object> map = new HashMap<>();
		map.put("number", onlyInt.getNumber());
		map.put("singleTaskId", onlyInt.getSingleTaskId());
		map.put("name", onlyInt.getName());
		map.put("systemName", onlyInt.getSystemName());
		map.put("handleDay", onlyInt.getHandleDay());
		map.put("handleMonth", onlyInt.getHandleMonth());
		map.put("handleYear", onlyInt.getHandleYear());
		map.put("deptPerson", onlyInt.getDeptPerson());
		map.put("personTel", onlyInt.getPersonTel());
		reportMolService.delete(onlyInt.getSingleTaskId());
		reportMolService.add(map);
	}
}
